package org.stone.project.common.pojo.vo;

import java.io.Serializable;

import lombok.Getter;
import lombok.Setter;
import org.stone.project.common.pojo.sysEnum.SystemCodeEnum;

/**
 * 通用结果集
 * @param <T>
 */
@Getter
@Setter
public class R<T> implements Serializable {
    /**
     * 系统编码
     */
    private Integer code;

    /**
     * 错误信息
     */
    private String msg;

    /**
     * 消息体
     */
    private T data;

    private String uuid;

    public static <T> R<T> ok()
    {
        return restResult(null, SystemCodeEnum.SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data)
    {
        return restResult(data, SystemCodeEnum.SUCCESS, "操作成功");
    }

    public static <T> R<T> ok(T data, String msg)
    {
        return restResult(data, SystemCodeEnum.SUCCESS, msg);
    }

    public static <T> R<T> fail()
    {
        return restResult(null, SystemCodeEnum.CUS_ERROR, "操作失败");
    }

    public static <T> R<T> fail(String msg)
    {
        return restResult(null, SystemCodeEnum.CUS_ERROR, msg);
    }

    public static <T> R<T> fail(T data)
    {
        return restResult(data, SystemCodeEnum.CUS_ERROR, "操作失败");
    }

    public static <T> R<T> fail(T data, String msg)
    {
        return restResult(data, SystemCodeEnum.CUS_ERROR, msg);
    }

    public static <T> R<T> fail(SystemCodeEnum code, String msg)
    {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, SystemCodeEnum code, String msg)
    {
        R<T> apiResult = new R<>();
        apiResult.setCode(code.getCode());
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }
}
